package com.zookeeper;

import org.apache.zookeeper.*;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;

public class Base {

    private static String CONNECT_ADDR=
            "192.168.1.121:2181";
    //"192.168.1.120:2181,192.168.1.121:2181,192.168.1.122:2181";
    private static int SESSION_OUTTIME=5000;//ms
    private static final CountDownLatch consemphore1=new CountDownLatch(1);
    //private static final CountDownLatch consemphore2=new CountDownLatch(1);

    @Test
    public  void test() throws Exception {
        //zookeeper的连接也是异步的连接
        ZooKeeper zkCli=new ZooKeeper(CONNECT_ADDR, SESSION_OUTTIME, new Watcher() {
            //连接成功之后会执行
            public void process(WatchedEvent event) {
                Event.KeeperState state = event.getState();
                Event.EventType eventType = event.getType();
                if (state.SyncConnected==state){
                    if (eventType.None==eventType){
                        System.out.println("连接成功");
                        consemphore1.countDown();
                    }
                }
            }
        });
        consemphore1.await();

        //持久化节点
        /*zkCli.create("/test","test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        //临时节点
        String path=zkCli.create("/children","children".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println(path);
        Thread.sleep(10000);*/

        //异步删除，(下面这些代码就是为了证明这个方法是异步执行的，应该是底层又开启了一个线程)
        /*zkCli.delete("/T1", -1, new AsyncCallback.VoidCallback() {
            public void processResult(int ro, String path, Object ctx) {
                try {
                    //进当前线程的sleep先保证主线程的执行
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(ro);
                System.out.println(path);
                System.out.println(ctx);
            }
        },"a");
        System.out.println("继续执行");*/
        //主线程等待那个线程执行完之后再执行，sleep的时间比那个长
        //比较打印结果的先后顺序，可以发现是两个线程异步执行的
        //Thread.sleep(5000);

        //获取单个节点数据
        /*byte[] bytes=zkCli.getData("/t1",false,null);
        System.out.println(new String(bytes));*/

        //获取多个节点数据(本质上也就是获取节点路径加上字符串的拼接，
        // 使用获得单个数据遍历的方式来获取多个节点数据)
        /*List<String> childrens = zkCli.getChildren("/t1", false);
        for (String path:childrens){
            System.out.println(path);
            String childrenPaht="/t1/"+path;
            System.out.println(new String(zkCli.getData(childrenPaht,false,null)));
        }*/

        //修改节点的数据
        /*zkCli.setData("/t1","update successful".getBytes(),-1);
        byte[] bytes=zkCli.getData("/t1",false,null);
        System.out.println(new String(bytes));*/

        //判断节点是否存在,存在的话返回的是一长串数字（不知道是啥）
        System.out.println(zkCli.exists("/t1",false));
        //删除节点
        zkCli.delete("/t1/children2",-1);
        //若不存在返回的空
        System.out.println(zkCli.exists("/t1/children3",false));

        zkCli.close();
    }
}
